package edu.chinasoft.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import edu.chinasoft.domain.Function;
import edu.chinasoft.service.FunctionService;

/**
 *
 * @author mhao
 * @date 2020年2月12日:下午3:56:48
 * @version V1.0
 * @Description: 
*/
@WebServlet(name = "myAddFunctionServlet", value = "/servlet/addfunctionservlet")
public class AddFunctionServlet extends HttpServlet {
	private static Logger logger = LoggerFactory.getLogger(AddFunctionServlet.class);
	private FunctionService functionService;
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info(" AddFunctionServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		logger.info(" AddFunctionServlet >doGet end.......");
	}

	/**
	 *
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info(" AddFunctionServlet >doPost start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String function_name = request.getParameter("function_name");
		String function_path = request.getParameter("function_path");
		String fatherFunctionName = request.getParameter("fatherFunction");
		logger.info("需插入的权限的父权限为："+fatherFunctionName);
		Function function = new Function();
		function.setFunctionName(function_name);
		function.setFunctionPath(function_path);
		Function fatherFunction = new Function();
		fatherFunction.setFunctionName(fatherFunctionName);
		function.setFatherFunction(fatherFunction);
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		FunctionService functionServiceImp = wac.getBean("functionService", FunctionService.class);
		this.functionService=functionServiceImp;
		boolean result = functionService.insertFunction(fatherFunction);
		if(result) {
			response.sendRedirect(""+request.getContextPath()+"/servlet/showfunctionservlet");
		}else {
			response.sendRedirect(""+request.getContextPath()+"/addfunctionerror.jsp");
		}
		logger.info(" AddFunctionServlet >doPost end.......");
	}

}